import org.codehaus.groovy.grails.plugins.springsecurity.Secured

// TODO: modify to allow user also.
@Secured(['ROLE_ADMIN', 'ROLE_POWER_USER'])
class OfficeHoursController {

    def authenticateService
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ officeHoursInstanceList: OfficeHours.list( params ), officeHoursInstanceTotal: OfficeHours.count() ]
    }

    def show = {
        def officeHoursInstance = OfficeHours.get( params.id )

        if(!officeHoursInstance) {
            flash.message = "OfficeHours not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ officeHoursInstance : officeHoursInstance ] }
    }

    def delete = {
        def officeHoursInstance = OfficeHours.get( params.id )
        if(officeHoursInstance) {
            try {
                officeHoursInstance.delete(flush:true)
                flash.message = "OfficeHours ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "OfficeHours ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "OfficeHours not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def officeHoursInstance = OfficeHours.get( params.id )

        if(!officeHoursInstance) {
            flash.message = "OfficeHours not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ officeHoursInstance : officeHoursInstance ]
        }
    }

    def update = {
        def officeHoursInstance = OfficeHours.get( params.id )
        if(officeHoursInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(officeHoursInstance.version > version) {
                    
                    officeHoursInstance.errors.rejectValue("version", "officeHours.optimistic.locking.failure", "Another user has updated this OfficeHours while you were editing.")
                    render(view:'edit',model:[officeHoursInstance:officeHoursInstance])
                    return
                }
            }
            officeHoursInstance.properties = params
            if(!officeHoursInstance.hasErrors() && officeHoursInstance.save()) {
                flash.message = "OfficeHours ${params.id} updated"
                redirect(action:show,id:officeHoursInstance.id)
            }
            else {
                render(view:'edit',model:[officeHoursInstance:officeHoursInstance])
            }
        }
        else {
            flash.message = "OfficeHours not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def officeHoursInstance = new OfficeHours()
        officeHoursInstance.properties = params
        return ['officeHoursInstance':officeHoursInstance]
    }

    def save = {
        def officeHoursInstance = new OfficeHours(params)
        if(!officeHoursInstance.hasErrors() && officeHoursInstance.save()) {
            flash.message = "OfficeHours ${officeHoursInstance.id} created"
            redirect(action:show,id:officeHoursInstance.id)
        }
        else {
            render(view:'create',model:[officeHoursInstance:officeHoursInstance])
        }
    }
}
